package org.jeecg.modules.cw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.cw.entity.CwJbszqkzb;
import org.jeecg.modules.cw.entity.CwJbszqkzbZhu;
import org.jeecg.modules.cw.entity.CwZyryzfgjjTj;
import org.jeecg.modules.cw.entity.CwZyryzfgjj;
import org.jeecg.modules.cw.mapper.CwZyryzfgjjMapper;
import org.jeecg.modules.cw.mapper.CwZyryzfgjjTjMapper;
import org.jeecg.modules.cw.service.ICwJbszqkzbService;
import org.jeecg.modules.cw.service.ICwJbszqkzbZhuService;
import org.jeecg.modules.cw.service.ICwZyryzfgjjTjService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 在职人员住房公积金测算明细提交表
 * @Author: jeecg-boot
 * @Date:   2025-09-09
 * @Version: V1.0
 */
@Service
public class CwZyryzfgjjTjServiceImpl extends ServiceImpl<CwZyryzfgjjTjMapper, CwZyryzfgjjTj> implements ICwZyryzfgjjTjService {

	@Autowired
	private CwZyryzfgjjTjMapper cwZyryzfgjjTjMapper;
	@Autowired
	private CwZyryzfgjjMapper cwZyryzfgjjMapper;
	@Autowired
	private ICwJbszqkzbService cwJbszqkzbService;
	@Autowired
	private ICwJbszqkzbZhuService cwJbszqkzbZhuService;
	@Autowired
	private ISysDepartService sysDepartService;
	@Autowired
	private ISysBaseAPI iSysBaseAPI;
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(CwZyryzfgjjTj cwZyryzfgjjTj, List<CwZyryzfgjj> cwZyryzfgjjList) {

		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String departIdsByOrgCode = iSysBaseAPI.getDepartIdsByOrgCode(user.getOrgCode());
		SysDepart byId = sysDepartService.getById(departIdsByOrgCode);
		String departName = byId.getDepartName();
		cwZyryzfgjjTj.setUnit(departName);
		cwZyryzfgjjTjMapper.insert(cwZyryzfgjjTj);
		Double acz=0.0;
		Double afcz=0.0;
		if(cwZyryzfgjjList!=null && cwZyryzfgjjList.size()>0) {
			for(CwZyryzfgjj entity:cwZyryzfgjjList) {
				//外键设置
				entity.setUnit(cwZyryzfgjjTj.getUnit());
				entity.setYear(cwZyryzfgjjTj.getYear());
				entity.setUserId(user.getId());
				entity.setMianId(cwZyryzfgjjTj.getId());

				if (entity.getPostWage()==null){
					entity.setPostWage(0.0);
				}
				if (entity.getIncrementalStep()==null){
					entity.setIncrementalStep(0.0);
				}
				if (entity.getLivingAllowance()==null){
					entity.setLivingAllowance(0.0);
				}
				if (entity.getWorkAllowance()==null){
					entity.setWorkAllowance(0.0);
				}
				if (entity.getSanitationFee()==null){
					entity.setSanitationFee(0.0);
				}if (entity.getHmbt()==null){
					entity.setHmbt(0.0);
				}
				if (entity.getJkbybt()==null){
					entity.setJkbybt(0.0);
				}
				if (entity.getHealthSubsidies()==null){
					entity.setHealthSubsidies(0.0);
				}
				if (entity.getHousingSubsidy()==null){
					entity.setHousingSubsidy(0.0);
				}
				if (entity.getJcjxj()==null){
					entity.setJcjxj(0.0);
				}
				if (entity.getNdkhj()==null){
					entity.setNdkhj(0.0);
				}
				if (entity.getApj()==null){
					entity.setApj(0.0);
				}
				if (entity.getWxjbt()==null){
					entity.setWxjbt(0.0);
				}
				if (entity.getZzyf()==null){
					entity.setZzyf(0);
				}
				//月应发合计
				if ("财政".equals(entity.getCzFcz())){
				Double hj=entity.getPostWage()+entity.getIncrementalStep()+entity.getLivingAllowance()
						+entity.getWorkAllowance()+entity.getSanitationFee()+entity.getHmbt()+
						entity.getJkbybt()+entity.getHealthSubsidies()+entity.getHousingSubsidy()+entity.getJcjxj();
// 先计算原始值
				double jjTemp = hj * entity.getZzyf() +
						(entity.getPostWage() + entity.getIncrementalStep() +
								entity.getNdkhj() + entity.getApj() + entity.getWxjbt()) / 12 * entity.getZzyf();
// 保留两位小数（四舍五入）
				Double jj = new BigDecimal(jjTemp)
						.setScale(2, RoundingMode.HALF_UP)  // 保留2位小数，四舍五入模式
						.doubleValue();
				entity.setSsx(entity.getPostWage()+entity.getIncrementalStep());
					entity.setGjjjfjs(jj);
				entity.setYjce(new BigDecimal(jj/entity.getZzyf()*0.12).setScale(0, RoundingMode.HALF_UP).doubleValue());
				entity.setNjce(new BigDecimal((jj/entity.getZzyf()*0.12)*entity.getZzyf()).setScale(0, RoundingMode.HALF_UP).doubleValue());
				}else {
					Double hj=entity.getPostWage()+entity.getIncrementalStep()+entity.getLivingAllowance()
							+entity.getWorkAllowance()+entity.getSanitationFee()+entity.getHmbt()+
							entity.getJkbybt()+entity.getHealthSubsidies()+entity.getHousingSubsidy()+entity.getJcjxj();
// 先计算原始值
					double jjTemp = hj * entity.getZzyf() +
							(entity.getPostWage() + entity.getIncrementalStep() +
									entity.getNdkhj() + entity.getApj() + entity.getWxjbt()) / 12 * entity.getZzyf();
// 保留两位小数（四舍五入）
					Double jj = new BigDecimal(jjTemp)
							.setScale(2, RoundingMode.HALF_UP)  // 保留2位小数，四舍五入模式
							.doubleValue();
					entity.setSsx(entity.getPostWage()+entity.getIncrementalStep());
					entity.setYyfhj(hj);
					entity.setGjjjfjs(jj);
					entity.setYjce(new BigDecimal(jj/entity.getZzyf()*0.12).setScale(0, RoundingMode.HALF_UP).doubleValue());
					entity.setNjce(new BigDecimal((jj/entity.getZzyf()*0.12)*entity.getZzyf()).setScale(0, RoundingMode.HALF_UP).doubleValue());
				}
				cwZyryzfgjjMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(CwZyryzfgjjTj cwZyryzfgjjTj,List<CwZyryzfgjj> cwZyryzfgjjList) {
		cwZyryzfgjjTjMapper.updateById(cwZyryzfgjjTj);
		Double acz=0.0;
		Double afcz=0.0;

		//1.先删除子表数据
		cwZyryzfgjjMapper.deleteByMainId(cwZyryzfgjjTj.getId());
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		//2.子表数据重新插入
		if(cwZyryzfgjjList!=null && cwZyryzfgjjList.size()>0) {
			for(CwZyryzfgjj entity:cwZyryzfgjjList) {
				if ("财政".equals(entity.getCzFcz())){
					Double hj=entity.getPostWage()+entity.getIncrementalStep()+entity.getLivingAllowance()
							+entity.getWorkAllowance()+entity.getSanitationFee()+entity.getHmbt()+
							entity.getJkbybt()+entity.getHealthSubsidies()+entity.getHousingSubsidy()+entity.getJcjxj();
// 先计算原始值
					double current = hj * entity.getZzyf() +
							(entity.getPostWage() + entity.getIncrementalStep() +
									entity.getNdkhj() + entity.getApj() + entity.getWxjbt()) / 12 * entity.getZzyf();
// 保留两位小数（四舍五入）
					Double jj = new BigDecimal(current)
							.setScale(2, RoundingMode.HALF_UP)  // 保留2位小数，四舍五入模式
							.doubleValue();
					entity.setSsx(entity.getPostWage()+entity.getIncrementalStep());
					entity.setGjjjfjs(jj);
					entity.setYjce(new BigDecimal(jj/entity.getZzyf()*0.12).setScale(0, RoundingMode.HALF_UP).doubleValue());
					entity.setNjce(new BigDecimal((jj/entity.getZzyf()*0.12)*entity.getZzyf()).setScale(0, RoundingMode.HALF_UP).doubleValue());

					acz =jj;
				}else {
					Double hj=entity.getPostWage()+entity.getIncrementalStep()+entity.getLivingAllowance()
							+entity.getWorkAllowance()+entity.getSanitationFee()+entity.getHmbt()+
							entity.getJkbybt()+entity.getHealthSubsidies()+entity.getHousingSubsidy()+entity.getJcjxj();
// 先计算原始值
					double current = hj * entity.getZzyf() +
							(entity.getPostWage() + entity.getIncrementalStep() +
									entity.getNdkhj() + entity.getApj() + entity.getWxjbt()) / 12 * entity.getZzyf();
// 保留两位小数（四舍五入）
					Double jj = new BigDecimal(current)
							.setScale(2, RoundingMode.HALF_UP)  // 保留2位小数，四舍五入模式
							.doubleValue();
					entity.setSsx(entity.getPostWage()+entity.getIncrementalStep());
					entity.setGjjjfjs(jj);
					entity.setYjce(new BigDecimal(jj/entity.getZzyf()*0.12).setScale(0, RoundingMode.HALF_UP).doubleValue());
					entity.setNjce(new BigDecimal((jj/entity.getZzyf()*0.12)*entity.getZzyf()).setScale(0, RoundingMode.HALF_UP).doubleValue());
					entity.setYyfhj(hj);
					afcz = jj;
				}
				//外键设置
				entity.setMianId(cwZyryzfgjjTj.getId());
				cwZyryzfgjjMapper.insert(entity);
			}
		}
//		LambdaQueryWrapper<CwJbszqkzb> wrapper = new LambdaQueryWrapper<>();
//		wrapper.eq(CwJbszqkzb::getYear,cwZyryzfgjjTj.getYear())
//				.eq(CwJbszqkzb::getUnit,cwZyryzfgjjTj.getUnit())
//				.in(CwJbszqkzb::getProject,"住房公积金");
//		long count = cwJbszqkzbService.count(wrapper);
//		if (count == 1) {
//			CwJbszqkzb one1 = cwJbszqkzbService.getOne(wrapper);
//			one1.setUnit(cwZyryzfgjjTj.getUnit());
//			one1.setYear(cwZyryzfgjjTj.getYear());
//			one1.setFinancial(acz);
//			one1.setNonFinancials(afcz);
//			if (one1.getYsshj()==null){
//				one1.setYsshj(0.0);
//			}
//			one1.setYsshj(acz+afcz);
//			one1.setCwPx(21);
//
//			cwJbszqkzbService.updateById(one1);
//		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		cwZyryzfgjjMapper.deleteByMainId(id);
		cwZyryzfgjjTjMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			cwZyryzfgjjMapper.deleteByMainId(id.toString());
			cwZyryzfgjjTjMapper.deleteById(id);
		}
	}
	
}
